﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.Models.DataEntity;

namespace bmapi.Controllers
{
    public class CAD22Controller : CADFinishController
    {
        /// <summary>
        /// 导入生产单   
        /// </summary> 
        /// <param name="json">id process go upload file文件名/文件夹名 extname扩展名  TDxml文件路径 </param>
        [HttpPost, Route("cad22/Finish")]
        public string Finish(dynamic json)
        {
            WriteLog("cad22/Finish", "json:"+json.ToString(), true);
            if (json == null)
                return ReturnError(201, "没有可执行参数");
            int loginid = GetCrrentIDByToken();
            User user = GetUserByLoginID(loginid);

            WriteDebug("cad22/Finish", "2");

            if (user == null)
                return ReturnError(201, "不存在当前工号为" + loginid + "的用户");

            //文件名实际上就是款式名
            string filename =json.file.ToString();

            // name实际上就是新样式名
            string modelName = json.name.ToString();
            string xmlpath = GetPCPath(modelName) + "\\" + filename;

            WriteDebug("cad22/Finish", "3");

            if (!File.Exists(xmlpath))
                return ReturnError(201, "找不到路径" + xmlpath);

            string styleName = Path.GetFileNameWithoutExtension(filename); //去掉扩展名".model"

            WriteDebug("cad22/Finish", "6");
            if (CheckModelname(modelName))
            {
                WriteDebug("cad22/Finish", modelName + "已存在");
                return ReturnError(201, modelName + "已存在");
            }

            if (!CheckStylename(styleName))
            {
                WriteDebug("cad22/Finish", styleName + "不存在");
                return ReturnError(201, styleName + "不存在");
            }

            if (AddModelAndPC(user, modelName, styleName) == 0)
            {
                WriteDebug("cad22/Finish", "7");
                return ReturnError(201, "操作数据库出错");
            }
            return ReturnError(200, "OK");
        }

        /// <summary>
        /// 添加生产单信息 
        /// </summary> 
        /// <param name="loginid"></param>
        /// <param name="modelName"></param>
        /// <param name="orderName"></param>
        /// <param name="orderID"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        private int AddModelAndPC(User user, string modelName, string styleName)
        {
            if (user == null)
                return 0;
            try
            {
                List<SQLCommand> cmds = new List<SQLCommand>();

                int modelid = GetNewSafeID(user.login_id);

                //int styleId = 0; //用styleName获得款式ID

                #region  新增样式   
                WriteDebug("AddModelAndPC", "modelid:" + modelid);

               
                //获取款式ID
                string sql = "select style_id from Style_tb where name=@name";
                SQLCommand style = new SQLCommand(sql,"@name",styleName);
                int styleId = Convert.ToInt32(style.ExecuteScalar());

                //PLID是当前用户对应的吗  暂时这样做处理  model_id 根据样板名从Model_tb表中获取 erpid为表中的orderID ername样板名 lsh流水号暂认为0
                //流水号  LSH LSH_tb中erpID指用户id即工号 根据用户id获取流水号
              
                sql = "insert into Model_tb values(";
                SQLCommand sc = new SQLCommand();
                sc.AddInsert("@modelid", modelid);
                sc.AddInsert("@modelname", modelName);
                sc.AddInsert("@creater", user.login_id);
                sc.AddTimeInsert("@time", DateTime.Now);
                sc.AddInsert("@img", "");
                sc.AddInsert("@sid", styleId);
                sc.AddInsert("@plid", user.PLID);
                sc.AddInsert("@pod", 0);
                sc.AddInsert("@state", 1);
                sc.JoinSql(sql);

                cmds.Add(sc);

                WriteDebug("AddModelAndPC", "Order插入语句");
                #endregion

                #region  添加流程
                
                sql = "insert into PC_tb(path,pc_id,PLID,maxStep,step,model_id,grouping) values(";
                sc = new SQLCommand();
                sc.AddInsert("@path", modelName);
                sc.AddInsert("@pcid", modelid);
                sc.AddInsert("@plid", user.PLID);
                sc.AddInsert("@maxstep", 22);
                sc.AddInsert("@step", GetNextProcessID(user.PLID, 22));
                sc.AddInsert("@model_id", modelid);
                sc.AddInsert("@grouping", user.grouping);
                sc.JoinSql(sql);
                //string strsql = SQLCommand.paras;   
                //sql += strsql.Remove(strsql.LastIndexOf(",")) + ")";
                //sc.cmd.CommandText = sql;  
                

                cmds.Add(sc);
                WriteDebug("AddOrderAndPC", "PC_tb插入语句");
                #endregion
                int rtn = SQLCommand.ExecTran(cmds);

                if (rtn == 1)
                {
                    WriteDebug("AddOrderAndPC", "新增Order与PC记录成功 rtn:" + rtn);
                    AddTask(22, modelid);
                    AddTask(23, modelid);
                }
                return rtn;
            }
            catch (Exception e)
            {

                WriteLog("AddOrderAndPC", e.Message);
                return 0;
            }

        }
    }
}
